package com.estate.business.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.estate.business.DeleProjectService;
import com.estate.dao.DeleProjectDao;
import com.estate.utils.DeleProjectUtil;
import com.estate.vo.Vo;
import com.estate.vo.impl.AnnouncementVo;
import com.estate.vo.impl.DeleProjectVo;
import com.estate.vo.impl.UserInfoVo;

public class DeleProjectServiceImpl implements DeleProjectService {

	private DeleProjectDao deleProjectDao;

	public DeleProjectDao getDeleProjectDao() {
		return deleProjectDao;
	}

	public void setDeleProjectDao(DeleProjectDao deleProjectDao) {
		this.deleProjectDao = deleProjectDao;
	}

	public boolean addDeleProject(DeleProjectVo deleProject) {
		boolean result =false; 
		result = this.getDeleProjectDao().addVo(deleProject);
		return result;
	}

	public List<DeleProjectVo> getMyDeleProjectList(UserInfoVo userInfo) {
		List<DeleProjectVo> allPostList = this.getDeleProjectDao().getAllPostList();
		List<DeleProjectVo> allMyUnPostedList = this.getDeleProjectDao().getMyUnPostedList(userInfo);
		
		List<DeleProjectVo> projectList = new ArrayList<DeleProjectVo>();
		projectList.addAll(allPostList);
		projectList.addAll(allMyUnPostedList);
		return projectList;
	}

	public DeleProjectVo getDeleProject(int id) {
		DeleProjectVo project = (DeleProjectVo) this.getDeleProjectDao().findByPK(id);
		return project;
	}

	public boolean updateDeleProject(DeleProjectVo deleProject) {
		DeleProjectVo old = this.getDeleProject(deleProject.getId());
		if(old.getState().equals("未发布")){
			if("已发布".equals(deleProject.getState())){
				deleProject.setPostDate(new Date());
			}
		}else{
			deleProject.setPostDate(old.getPostDate());
		}
		deleProject.setPostUser(old.getPostUser());
		this.getDeleProjectDao().updateVo(deleProject);
		return true;
	}
	
	public boolean updatePost(int id) {
		DeleProjectVo old = (DeleProjectVo) this.getDeleProject(id);
		old.setState(DeleProjectUtil.getDeleProjectString(2));
		old.setPostDate(new Date());
		boolean result = this.getDeleProjectDao().updateVo(old);
		return result;
	}
	
	public boolean deleteMultiDeleProject(String delStr) {
		if(delStr!= null && !"".equals(delStr) && !",".equals(delStr)){
			String[] delArray = delStr.split(",");
			DeleProjectVo deleProject = null;
			for(int i = 0;i<delArray.length;i++){
				deleProject = new DeleProjectVo();
				deleProject.setId(Integer.parseInt(delArray[i]));
				this.deleteDeleProject(deleProject);
			}
		}
		return true;
	}
	
	public boolean deleteDeleProject(DeleProjectVo deleProject){
		boolean result = false;
		result = this.getDeleProjectDao().deleteVo(deleProject);
		return result;
	}

	public List<Vo> findAllDeleProject() {
		// TODO Auto-generated method stub
		return deleProjectDao.findAll();
	}
}
